#!/usr/bin/python
#encoding UTF-8

import os
import sys
import re
from  testUnit import tester

class swift():

    __Excute = None

    def __init__(self):
        self.__Excute = os.system
        return

    def install(self,ips):
        for ip in ips:
            self.__Excute("ssh "+ ip +" cp -r /opt/tomcat-cloud-web /opt/tomcat-swift")
            self.__Excute("ssh "+ ip +" mv -f /opt/tomcat-swift/conf/server.xml.bak  /opt/tomcat-swift/conf/server.xml")
            self.__Excute("ssh "+ ip +" mv -f /opt/tomcat-swift/bin/setenv.sh.bak  /opt/tomcat-swift/bin/setenv.sh")
            self.__Excute("ssh "+ ip +" rm -rf /opt/tomcat-swift/webapps/cloud-web")
            self.__Excute("scp -r /opt/iop/resource/web/v1/ root@"+ip+":/opt/tomcat-swift/webapps/")
        return

    def config(self,ips,proxy,ssl,commonPass):
        if not os.path.exists("/tmp/iop/swift"):
            self.__Excute("mkdir -p /tmp/iop/swift")
        self.__Excute("rm -rf  /tmp/iop/swift/*")
        reader = open("./config/swift/swift.cfg","r")
        data = reader.readlines()
        reader.close()
        for d in data:
            temp = d.split("!!!")
            filename = temp[0]
            temp = temp[1].split("&")
            flag = temp[1]
            temp = temp[0].split("$")
            atributes = temp
            self.configure(ips,proxy,atributes,filename,flag,ssl,commonPass)
            for ip in ips:
                self.__Excute("scp /tmp/iop/swift/"+filename+" root@"+ip+":/opt/tomcat-swift/webapps/v1/WEB-INF/classes/")
        return

    def configure(self,ips,proxy,atributes,filename,flag,ssl,commonPass):
        if os.path.exists("/tmp/iop/swift/"+filename):
            if os.path.exists("/tmp/iop/swift/"+filename+".tmp"):
                self.__Excute("rm -rf /tmp/iop/swift/"+filename+".tmp")
            self.__Excute("cp /tmp/iop/swift/"+filename+" /tmp/iop/swift/"+filename+".tmp")
            reader = open("/tmp/iop/swift/"+filename+".tmp","r")
        else:
            reader = open("./resource/web/v1/WEB-INF/classes/"+filename,"r")
        data  = reader.readlines()
        reader.close()
        writer = open("/tmp/iop/swift/"+filename,"w")
        j = 0
        for atribute in atributes:
            for i in range(j,len(data)):
                temp = atribute.split("=")
                if re.search(temp[0],data[i]):
                    if flag == "proxy\n":
                        if ssl:
                            atribute = re.sub("http","https",atribute)
                        writer.write(re.sub("X",proxy,atribute)+"\n")
                        j =i+1
                        break
                    elif flag == "password\n":
                            writer.write(re.sub(r"\${password}(\n)?",commonPass,data[i])+"\n")
                            j =i+1
                            break
                    else:
                        print "ERROR:=======================>> no flags!!!!!"
                else:
                    writer.write(data[i])
        if j != len(data)-1:
            for i in range(j,len(data)):
                writer.write(data[i])
        writer.close()
        return

    def run(self,ips):
        for ip in ips:
            self.__Excute("ssh "+ip+" \"export PATH=\$PATH:/opt/jdk/bin;export JAVA_HOME=/opt/jdk;/opt/tomcat-swift/bin/startup.sh\"")

if __name__ == "__main__":
    T = tester()
    if not T.test():
        raise Exception("less arguments or illegal arguments")
    ips = T.get_ips()
    O = swift()
    O.install(ips)
    O.config(ips,"10.110.17.136")
